let webshot = require('node-webshot');
let xlsx = require('node-xlsx');
let fs = require('fs');
let sheets = xlsx.parse('./new.xls');

let arr = [];
sheets.forEach(function (sheet) {
    var newSheetsArr = [];
    for (var i = 2; i < 50; i++) {
        var row = sheet['data'][i];
        if (row && row.length > 0) {
            let linkData = row[7];
            let count = row[8];
            let linkStr = ''
            linkStr = linkData.substring(linkData.lastIndexOf('http'))
            newSheetsArr.push({
                class:row[2],
                title: row[3],
                link: row[7],
                count: row[8],
                linkStr: linkStr
            });
        }
    }
    arr.push(newSheetsArr);
});
let shotPromise = function (item) {
    return new Promise(resolve => {
        webshot(item.linkStr, `./${item.class}/${item.title}.png`, {
            shotSize: {
                width: 'all', height: 'all'
            },
            // phantomConfig: {'ignore-ssl-errors': 'true'},
            quality: 80,
            renderDelay: 1000,
            timeout:10000,
            errorIfStatusIsNot200: true,
            // userAgent: 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_2 like Mac OS X; en-us)'
            // + ' AppleWebKit/531.21.20 (KHTML, like Gecko) Mobile/7B298g'
        }, function (err) {
            if (err) {
                console.log(err)
                fs.appendFile("./error.txt",`${item.class}\r\n ${item.title}\r\n${item.linkStr}\r\n========\r\n` , (error)  => {});
                console.log(item.title + '====error====' + item.linkStr)
            }else{
                console.log(item.title + '====success===='+ item.linkStr)
            }
            resolve();
        });
    })
}

let goShot = async function () {
    fs.writeFile('./error.txt','','utf8',(err)=>{})
    for (let index = 0; index < arr[0].length; index++) {
        let item = arr[0][index];
        await shotPromise(item)
        console.log('loading.....'+(index+1)+'/'+arr[0].length)
    }
}
goShot();
